/**************************************************************************
 *
 * Copyright (C) 2007 University of California, Los Angeles.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *************************************************************************/

package edu.ucla.cs.typecast.rmi;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.lang.reflect.Method;

import edu.ucla.cs.typecast.rmi.runtime.RMIUtil;


public class InvocationUnmarshaller {

	/**
	 * This class deserializes a method invocation from a byte stream. 
	 * @param inputStream The input stream that contains the serialized method invocation. 
	 * @return The method invocation that is unmarshalled from the byte stream. 
	 * @throws IOException
	 */
	public MethodInvocation unmarshall(InputStream inputStream) throws IOException { 
		try {
			ObjectInputStream dataIn = new ObjectInputStream(inputStream);
			//@TODO: add bloom filter parsing here
			Class[] types = (Class[]) dataIn.readObject();
			Long corID = (Long)dataIn.readObject(); 
			Class declaringType = (Class)dataIn.readObject(); 
			String methodName = (String)dataIn.readObject(); 			
			Class[] params = (Class[])dataIn.readObject(); 
			Method method = declaringType.getMethod(methodName, params); 
			Object[] args = (Object[]) dataIn.readObject();		
			return new  MethodInvocation(types, method, args, corID); 
		} catch(Throwable t) {
			t.printStackTrace();
			throw new RuntimeException(t); 
		}
	}	
}
